{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4.1 逻辑回归模型算法原理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**4.1.1 逻辑回归模型的数学原理**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8+yak3AAAACXBIWXMAAAsTAAALEwEAmpwYAAAgM0lEQVR4nO3deXyU9b328c83+wIJS8IWlrCFVQGNuLVugIK24PO0Vjlt1dZWa49Wq6cWa5dT2vOc2tpqtZ4qtdXWatGirVgVFBe0tiBBECQshoQlYUmAkH2Z5ff8kehJkWVIJrlnud6v17xm5p6bzHUTcvHLb+7FnHOIiEj0S/A6gIiIhIcKXUQkRqjQRURihApdRCRGqNBFRGJEkldvnJOT4/Lz8716exGRqLR27doDzrnco73mWaHn5+dTVFTk1duLiEQlM9t5rNc05SIiEiNU6CIiMUKFLiISI1ToIiIxQoUuIhIjTljoZvY7M6s0s/eP8bqZ2f1mVmJmG8zstPDHFBGREwllhP4YMPs4r88Bxrbfrgd+3fVYIiJysk64H7pz7k0zyz/OKvOAP7i28/CuMrM+ZjbYObc3XCFFRLzmnKPFH6TZF6DZ137vD9DiC9IaCNLqD9LiD7Tftz33BRz+YIfHgSC+QJAZEwYyZVifsGcMx4FFecDuDs/L25d9rNDN7HraRvEMHz48DG8tInJ8zjmafAGqG30cbmzlcKOPw40+apt91DX7qG/2U9vsp67ZT12zj8bWAA2tfpo63rcEaPIFwpZpYHZaxBZ6yJxzi4BFAIWFhbqyhoh0WiDoqKxrpqK6iX21zVTVtfzvrb7t/kB9C9WNPlr9wWN+HTPolZJE77Qkeqclk5maSGZKErm9UslMTSI9JZHMl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ezCYfZ2a9gGeAW51ztV7nOVlm9img0jm31swu8DhOVyUBpwE3O+dWm9kvgQXA98L5BhHLOTfzWK+Z2Y3As+0F/o6ZBWk74U1VT+U7GcfaFjM7hbb/ud8zM2ibpnjXzKY75/b1YMSQHO97AmBm1wKfAmZE6n+uxxHKBdGjhpkl01bmTzjnnvU6TyedC8w1s0uBNCDLzP7onPuCx7k6oxwod859+JvSEtoKPWyiecrlr8CFAGZWAKQQhWdic85tdM4NcM7lO+fyafumnxaJZX4iZjabtl+N5zrnGr3O0wmhXBA9Kljb6OC3wGbn3C+8ztNZzrk7nXND2382rqLtAvTRWOa0/0zvNrNx7YtmAMXhfI+IHqGfwO+A35nZ+0ArcE0Ujghjza+AVOCV9t82VjnnvuZtpNAd64LoHsfqrHOBLwIbzWx9+7LvtF8fWLxzM/BE+4ChFPhSOL+4Dv0XEYkR0TzlIiIiHajQRURihApdRCRGqNBFRGKECl1EJEao0EVEYoQKXUQkRvx/GnVn9PkJn2UAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 补充知识点：Sigmoid函数绘制\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "x = np.linspace(-6, 6)  # 通过linspace()函数生成-6到6的等差数列，默认50个数\n",
    "y = 1.0 / (1.0 + np.exp(-x))  # Sigmoid函数计算公式，exp()函数表示指数函数\n",
    "\n",
    "plt.plot(x,y)  # 画图\n",
    "plt.show()  # 展示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-6.        , -5.75510204, -5.51020408, -5.26530612, -5.02040816,\n",
       "       -4.7755102 , -4.53061224, -4.28571429, -4.04081633, -3.79591837,\n",
       "       -3.55102041, -3.30612245, -3.06122449, -2.81632653, -2.57142857,\n",
       "       -2.32653061, -2.08163265, -1.83673469, -1.59183673, -1.34693878,\n",
       "       -1.10204082, -0.85714286, -0.6122449 , -0.36734694, -0.12244898,\n",
       "        0.12244898,  0.36734694,  0.6122449 ,  0.85714286,  1.10204082,\n",
       "        1.34693878,  1.59183673,  1.83673469,  2.08163265,  2.32653061,\n",
       "        2.57142857,  2.81632653,  3.06122449,  3.30612245,  3.55102041,\n",
       "        3.79591837,  4.04081633,  4.28571429,  4.53061224,  4.7755102 ,\n",
       "        5.02040816,  5.26530612,  5.51020408,  5.75510204,  6.        ])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 演示下linespace()函数\n",
    "import numpy as np\n",
    "x = np.linspace(-6, 6)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.718281828459045"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 演示下np.exp()函数\n",
    "x = -1 \n",
    "np.exp(-x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**4.1.2 逻辑回归模型的代码实现**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LogisticRegression()"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 构造数据\n",
    "X = [[1, 0], [5, 1], [6, 4], [4, 2], [3, 2]]\n",
    "y = [0, 1, 1, 0, 0]\n",
    "\n",
    "# 模型训练\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "model = LogisticRegression()\n",
    "model.fit(X, y)  # 如果运行时下面出现FutureWarning警告，不要在意，它只是在告诉你以后模型的官方默认参数会有所调整而已，不是报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 如果不想看到FutureWarning这样的警告信息，可以在代码最上面加上如下内容\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0]\n"
     ]
    }
   ],
   "source": [
    "# 模型预测 - 预测单个数据\n",
    "print(model.predict([[2,2]]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 0 1]\n"
     ]
    }
   ],
   "source": [
    "# 模型预测 - 预测多个数据1\n",
    "print(model.predict([[1,1], [2,2], [5, 5]]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 1 0 0]\n"
     ]
    }
   ],
   "source": [
    "# 模型预测 - 预测多个数据2\n",
    "print(model.predict([[1, 0], [5, 1], [6, 4], [4, 2], [3, 2]]))  # 因为这里演示的多个数据和X是一样的，所以也可以直接写成model.predict(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以看到其预测准确度为100%。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**4.1.3 逻辑回归模型的深入理解**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.97344854, 0.02655146],\n",
       "       [0.39071972, 0.60928028],\n",
       "       [0.17991028, 0.82008972],\n",
       "       [0.63167893, 0.36832107],\n",
       "       [0.82424527, 0.17575473]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 预测概率：左列是分类为0的概率，右列是分类为1的概率\n",
    "y_pred_proba = model.predict_proba(X)\n",
    "y_pred_proba  # 直接打印"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>分类为0的概率</th>\n",
       "      <th>分类为1的概率</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.973449</td>\n",
       "      <td>0.026551</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.390720</td>\n",
       "      <td>0.609280</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.179910</td>\n",
       "      <td>0.820090</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.631679</td>\n",
       "      <td>0.368321</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.824245</td>\n",
       "      <td>0.175755</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    分类为0的概率   分类为1的概率\n",
       "0  0.973449  0.026551\n",
       "1  0.390720  0.609280\n",
       "2  0.179910  0.820090\n",
       "3  0.631679  0.368321\n",
       "4  0.824245  0.175755"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 另外一种打印概率的方式：通过DataFrame展示，更加好看些\n",
    "import pandas as pd\n",
    "a = pd.DataFrame(y_pred_proba, columns=['分类为0的概率', '分类为1的概率'])  # 2.2.1 通过numpy数组创建DataFrame\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1.00595248 0.02223835]]\n",
      "[-4.60771284]\n"
     ]
    }
   ],
   "source": [
    "# 打印系数和截距项\n",
    "print(model.coef_)  # 系数k1与k2\n",
    "print(model.intercept_)  # 截距项k0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.00595248],\n",
       "       [0.02223835]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.coef_.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.02655146]\n",
      "[0.60928028]\n",
      "[0.82008972]\n",
      "[0.36832107]\n",
      "[0.17575473]\n"
     ]
    }
   ],
   "source": [
    "# 如果想批量查看预测概率\n",
    "import numpy as np\n",
    "for i in range(5):  # 这里共有5条数据，所以循环5次\n",
    "    print(1 / (1 + np.exp(-(np.dot(X[i], model.coef_.T) + model.intercept_))))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**补充知识点：多分类逻辑回归模型演示**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LogisticRegression()"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 构造数据，此时y有多个分类\n",
    "X = [[1, 0], [5, 1], [6, 4], [4, 2], [3, 2]]\n",
    "y = [-1, 0, 1, 1, 1]  # 这里有三个分类-1、0、1\n",
    "\n",
    "# 模型训练\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "model = LogisticRegression()\n",
    "model.fit(X, y)  # 如果运行时下面出现FutureWarning警告，不要在意，它只是在告诉你以后模型的官方默认参数会有所调整而已，不是报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-1]\n"
     ]
    }
   ],
   "source": [
    "print(model.predict([[0, 0]]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1,  0,  1,  1,  1])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.predict(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.88352311 0.02340026 0.09307662]]\n"
     ]
    }
   ],
   "source": [
    "print(model.predict_proba([[0, 0]]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
